from django import forms

from user.models import UserInfo


class RegisterForm(forms.Form):
    username = forms.CharField(label='用户名:', max_length=8, min_length=3,
                               error_messages={
                                   'required': '用户名不能为空',
                                   'min_length': '用户名不能少于3位',
                                   'max_length': '用户名不能超过8位'

                               },
                               widget=forms.TextInput(attrs={
                                   "class": "form-control",
                                   "placeholder": "请输入用户名",
                               })
                               )
    password = forms.CharField(label='密码:', max_length=6, min_length=3,
                               error_messages={
                                   'required': '密码不能为空',
                                   'min_length': '密码不能少于3位',
                                   'max_length': '密码不能超过6位'
                               },
                               widget=forms.PasswordInput(attrs={
                                   "class": "form-control",

                                   "placeholder": "请输入密码",
                               })
                               )

    confirm_password = forms.CharField(label='确认密码:', max_length=6, min_length=3,
                                       error_messages={
                                           'required': '密码不能为空',
                                           'min_length': '密码不能少于3位',
                                           'max_length': '密码不能超过6位'
                                       },
                                       widget=forms.PasswordInput(attrs={
                                           "class": "form-control",
                                           "placeholder": "请输入确认密码",
                                       })
                                       )

    email = forms.EmailField(label='邮箱:',
                             error_messages={
                                 'required': '邮箱不能为空',
                                 'invalid': '邮箱格式错误'
                             },
                             widget=forms.EmailInput(attrs={
                                 "class": "form-control",
                                 "placeholder": "请输入邮箱",
                             })
                             )

    def clean_username(self):
        username = self.cleaned_data.get('username')
        user_exist = UserInfo.objects.filter(username=username).exists()
        if user_exist:
            self.add_error('username', f'当前用户{username}已存在')
        return username

    def clean(self):
        password = self.cleaned_data.get('password')
        confirm_password = self.cleaned_data.get('confirm_password')
        if password != confirm_password or not password or not confirm_password:
            self.add_error('password', '两次密码不一致')
            self.add_error('confirm_password', '两次密码不一致')
        return self.cleaned_data
